<?php

namespace app\index\controller;

use app\BaseController;
use app\index\model\PhotoList as PhotoListModal;
use think\facade\Filesystem;
class Index extends BaseController
{
    public function index()
    {
        return '我是前台接口11';
    }

    public function log()
    {

    }

    public function hello($name = 'ThinkPHP8')
    {
        return 'hello,' . $name;
    }

    public function star()
    {
//        $arr = [
//            [
//                'path' => 'E:\套图\fulitu\风之领域',
//                'json' => '风之领域.json'
//            ],
//            [
//                'path' => 'E:\套图\fulitu\紧急企划',
//                'json' => '紧急企划.json'
//            ],
//            [
//                'path' => 'E:\套图\fulitu\喵糖映画',
//                'json' => '喵糖映画.json'
//            ],
//            [
//                'path' => 'E:\套图\fulitu\喵写真',
//                'json' => '喵写真.json'
//            ],
//            [
//                'path' => 'E:\套图\fulitu\轻兰映画',
//                'json' => '轻兰映画.json'
//            ],
//            [
//                'path' => 'E:\套图\fulitu\森萝财团',
//                'json' => '森萝财团.json'
//            ],
//            [
//                'path' => 'E:\套图\fulitu\少女秩序',
//                'json' => '少女秩序.json'
//            ],
//            [
//                'path' => 'E:\套图\fulitu\微博网红\1-9',
//                'json' => '微博网红1-9.json'
//            ],
//            [
//                'path' => 'E:\套图\fulitu\微博网红\10-19',
//                'json' => '微博网红10-19.json'
//            ],
//            [
//                'path' => 'E:\套图\fulitu\微博网红\20-29',
//                'json' => '微博网红20-29.json'
//            ],
//            [
//                'path' => 'E:\套图\fulitu\微博网红\30-39',
//                'json' => '微博网红30-39.json'
//            ],
//            [
//                'path' => 'E:\套图\fulitu\微博网红\40-49',
//                'json' => '微博网红40-49.json'
//            ],
//            [
//                'path' => 'E:\套图\fulitu\微博网红\50-59',
//                'json' => '微博网红50-59.json'
//            ],
//            [
//                'path' => 'E:\套图\fulitu\微博网红\60-69',
//                'json' => '微博网红60-69.json'
//            ],
//            [
//                'path' => 'E:\套图\fulitu\微博网红\70-79',
//                'json' => '微博网红70-79.json'
//            ],
//            [
//                'path' => 'E:\套图\fulitu\微博网红\80-89',
//                'json' => '微博网红80-89.json'
//            ],
//            [
//                'path' => 'E:\套图\fulitu\微博网红\90-94',
//                'json' => '微博网红90-94.json'
//            ],
//            [
//                'path' => 'E:\套图\fulitu\稚乖画册',
//                'json' => '稚乖画册.json'
//            ],
//        ];
        $arr = [
            [
                'path' => 'E:\套图\jk.rs\jk',
                'json' => 'jk.json'
            ]
//            [
//                'path' => 'F:\静态\jk.rs\情绪',
//                'json' => '情绪.json'
//            ],
//            [
//                'path' => 'E:\套图\jk.rs\日系',
//                'json' => '日系.json'
//            ],
//            [
//                'path' => 'E:\套图\jk.rs\小清新',
//                'json' => '小清新.json'
//            ]
        ];
        foreach ($arr as $key => $item) {
            $this->test($item['path'], $item['json']);
        }
    }
    protected function get_name_by_append($array, $append) {
        foreach ($array as $key => $item) {
            if ($item['append'] == $append) {
                return $item['name'];
            }
        }
        return null; // 如果找不到匹配的append，返回null
    }
    protected function getPhotoNum($title)
    {
        if(strpos($title, '?')) {
            $names = explode("?", $title);
            $count = count($names);
            $newName = $names[$count-1];
            if(strpos($title, '-')) {
                $names = explode("-", $newName);
                $count = count($names);
                $newName = $names[$count-1];
            }
            return intval(str_replace('P', "", $newName));
        } else {
            $names = explode("-", $title);
            $count = count($names);
            return intval(str_replace('P', "", $names[$count-1]));
        }
    }
    protected function saveImg($name, $photoCount, $zipSize)
    {
        $whereArr[] = ["name", "=", $name];
        $sql = new PhotoListModal();
        $info = $sql->where($whereArr)->findOrEmpty();
        if($info->isEmpty()) {
            echo $name."不存在";
            echo "<br /";
        } else {
            $info->video_count = 0;
            $info->photo_count = $photoCount;
            $info->zipSize = $zipSize;
            $info->save();
            echo "保存成功".$info->id;
            echo "<br /";
        }

    }
    protected function getLength($title)
    {
        $length = 6;
        $names = explode("-", $title);
        if(intval($names[0]) < 10) {
            $length = $length - 1;
        }
        if(intval($names[1]) < 10) {
            $length = $length - 1;
        }
        return $length;
    }
    protected function test($folderPath, $jsonPath) {
        $folders = glob($folderPath . '/*', GLOB_ONLYDIR);
        $localArr = [];
        foreach ($folders as $folder) {
            $append = substr(basename($folder), 0, 5);
            $localArr[] = [
                'name' => basename($folder),
                'append' => $append
            ];
        }
        $content = file_get_contents('json/'.$jsonPath);
        $data = json_decode($content, true);
        foreach ($data as $key => $item) {
            $kb = floatval($item['size']);
            $titleLenth = $this->getLength(basename($item['name']));
            $jsonAppend = substr(basename($item['name']), 0, 5);
            $data[$key]['zipSize'] = number_format($kb / 1024 / 1024, 2).'M';
            $data[$key]['photo_count'] =  $this->getPhotoNum($item['name']);
            $newName = $this->get_name_by_append($localArr, $jsonAppend);
            if($newName) {
                $item['name'] = $newName;
                $item['title'] = $newName;
            }
            $data[$key]['title'] = mb_substr($newName, $titleLenth);
            $data[$key]['video_count'] =  0;
//            echo $data[$key]['title'];
//            echo "<br />";
//            echo $data[$key]['photo_count'];
//            echo "<br />";
            $this->saveImg($data[$key]['title'], $data[$key]['photo_count'], $data[$key]['zipSize']);
        }
    }
}
